package com.example.slidemenu;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.anim.CloseAnimation;
import com.example.anim.ExpandAnimation;

public class MainActivity extends Activity implements OnClickListener {

	/* slide menu */
	private DisplayMetrics metrics;
	private LinearLayout slidingPanel;
	private LinearLayout leftMenuPanel;	//private RelativeLayout rightMenuPanel;
	private FrameLayout.LayoutParams slidingPanelParameters;
	private FrameLayout.LayoutParams leftMenuPanelParameters;	//private FrameLayout.LayoutParams rightMenuPanelParameters;
	private int panelWidth;
	private static boolean isLeftExpanded;	//public static boolean isRightExpanded;
	private Button bt_left;//, bt_right;
	private Button bt_me;
	private TextView textman;
	private boolean isPopUp;
	private float slideLength=0.35f;
	private leftSlideMenu lSM;
	private PopupWindow mPopupWindow;
	//private LinearLayout searchPanel;
	//private SlideMenuClass smc;
	private Button bt1;
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainactivity);
		
		isPopUp=false;
		// �븸�뀡諛붾�� 媛��졇�솕�떎
		ActionBar bar = getActionBar();
		
		// 酉곕�� 媛��졇�샂
		View v = getLayoutInflater().inflate(R.layout.custombar, null);
		
		// �븸�뀡諛붿뿉 而ㅼ뒪��酉곕�� �꽕�젙
		bar.setCustomView(v, new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT));
		
		// 而ㅼ뒪�� 酉곕�� �뜥�빞�븯誘�濡� �샃�뀡�뿉�꽌 �꽕�젙
		bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		
		//setActionbarTitle();
		
		lSM=new leftSlideMenu();
		
		bt1 = (Button)findViewById(R.id.bt1);
		bt1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v)
			{
				Log.i("onClick", "CallSubActivity");
				Intent intentSubActivity = 
						new Intent(MainActivity.this, searchPage.class);
				setContentView(R.layout.searchpage);
			}
		
		});
		metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		panelWidth = (int) ((metrics.widthPixels) * slideLength);

		bt_left = (Button) findViewById(R.id.bt_left);
		bt_me = (Button) findViewById(R.id.bt_me);		//bt_right = (Button) findViewById(R.id.bt_right);
		textman = (TextView) findViewById(R.id.textman);
		
		
		
		bt_left.setOnClickListener(this);
		bt_me.setOnClickListener(this);		//bt_right.setOnClickListener(this);
		
		View ic_leftslidemenu = (View) findViewById(R.id.ic_leftslidemenu);
		
		// sliding view Initialize
		slidingPanel = (LinearLayout) findViewById(R.id.slidingPanel);
		slidingPanelParameters = (FrameLayout.LayoutParams) slidingPanel
				.getLayoutParams();
		slidingPanelParameters.width = metrics.widthPixels;
		slidingPanel.setLayoutParams(slidingPanelParameters);

		// left slide menu initialize
		leftMenuPanel = (LinearLayout) findViewById(R.id.leftMenuPanel);
		leftMenuPanelParameters = (FrameLayout.LayoutParams) leftMenuPanel
				.getLayoutParams();
		leftMenuPanelParameters.width = panelWidth;
		leftMenuPanel.setLayoutParams(leftMenuPanelParameters);

		// right slide menu initialize
		/*
		rightMenuPanel = (RelativeLayout) findViewById(R.id.rightMenuPanel);
		rightMenuPanelParameters = (FrameLayout.LayoutParams) rightMenuPanel
				.getLayoutParams();
		rightMenuPanelParameters.width = panelWidth;
		rightMenuPanel.setLayoutParams(rightMenuPanelParameters);
		*/
	}

	private void setActionbarTitle() {
		// TODO �옄�룞 �깮�꽦�맂 硫붿냼�뱶 �뒪�뀅
		TextView v = (TextView) findViewById(R.id.textman);
		v.setText("�떖�쁺�씠");
	}

	/**
	 * �넫�슣瑜� 筌롫뗀�뤀 占쎌쥒占쏙옙占쏙㎗�꼶�봺
	 */
	void menuLeftSlideAnimationToggle() {

		if (!isLeftExpanded) {

			// networkRequestTimeLineGetNewCnt();

			isLeftExpanded = true;
			//rightMenuPanel.setVisibility(View.GONE);
			leftMenuPanel.setVisibility(View.VISIBLE);

			// Expand
			new ExpandAnimation(slidingPanel, panelWidth, "left",
					Animation.RELATIVE_TO_SELF, 0.0f,
					Animation.RELATIVE_TO_SELF, slideLength, 0, 0.0f, 0, 0.0f);

			// disable all of main view
			// LinearLayout viewGroup = (LinearLayout) findViewById(
			FrameLayout viewGroup = (FrameLayout) findViewById(R.id.ll_fragment)
					.getParent();
			enableDisableViewGroup(viewGroup, false);

			// enable empty view
			((LinearLayout) findViewById(R.id.ll_empty))
					.setVisibility(View.VISIBLE);

			findViewById(R.id.ll_empty).setEnabled(true);
			findViewById(R.id.ll_empty).setOnTouchListener(
					new OnTouchListener() {

						@Override
						public boolean onTouch(View arg0, MotionEvent arg1) {
							menuLeftSlideAnimationToggle();
							return true;
						}
					});

		} else {
			isLeftExpanded = false;

			// Collapse
			new CloseAnimation(slidingPanel, panelWidth,
					TranslateAnimation.RELATIVE_TO_SELF, slideLength,
					TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0, 0.0f);

			// enable all of main view
			// LinearLayout viewGroup = (LinearLayout) findViewById(
			FrameLayout viewGroup = (FrameLayout) findViewById(R.id.ll_fragment)
					.getParent();
			enableDisableViewGroup(viewGroup, true);

			// disable empty view
			((LinearLayout) findViewById(R.id.ll_empty))
					.setVisibility(View.GONE);
			findViewById(R.id.ll_empty).setEnabled(false);

		}
	}

	/**
	 * 占썬끇�뀲筌잞옙筌롫뗀�뤀 占쎌쥒占쏙옙占쏙옙占쎈립 筌ｌ꼶�봺
	 */
	/*
	public void menuRightSlideAnimationToggle() {
		if (!isRightExpanded) {
			isRightExpanded = true;
			rightMenuPanel.setVisibility(View.VISIBLE);
			leftMenuPanel.setVisibility(View.GONE);

			// Expand
			new ExpandAnimation(slidingPanel, panelWidth, "right",
					Animation.RELATIVE_TO_SELF, 0.0f,
					Animation.RELATIVE_TO_SELF, -0.75f, 0, 0.0f, 0, 0.0f);

			FrameLayout viewGroup = (FrameLayout) findViewById(R.id.ll_fragment)
					.getParent();
			enableDisableViewGroup(viewGroup, false);

			// enable empty view
			((LinearLayout) findViewById(R.id.ll_empty))
					.setVisibility(View.VISIBLE);

			findViewById(R.id.ll_empty).setEnabled(true);
			findViewById(R.id.ll_empty).setOnTouchListener(
					new OnTouchListener() {

						@Override
						public boolean onTouch(View arg0, MotionEvent arg1) {
							menuRightSlideAnimationToggle();
							return true;
						}
					});

		} else {
			isRightExpanded = false;

			// Collapse
			new CloseAnimation(slidingPanel, panelWidth,
					TranslateAnimation.RELATIVE_TO_SELF, -0.75f,
					TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0, 0.0f);

			// enable all of main view
			FrameLayout viewGroup = (FrameLayout) findViewById(R.id.ll_fragment)
					.getParent();
			enableDisableViewGroup(viewGroup, true);

			// disable empty view
			((LinearLayout) findViewById(R.id.ll_empty))
					.setVisibility(View.GONE);
			findViewById(R.id.ll_empty).setEnabled(false);
		}
	}
*/
	/**
	 * �뀎怨쀬벥 占쎌늿�삂占쏙옙占쎌뮇堉깍옙�뮆�뼄. 占쎌꼷�맄 筌뤴뫀諭� �뀎怨뺣굶占쏙옙enable 揶쏅�れ몵嚥∽옙占썬끉�젟占쎌뮆�뼄.
	 * 
	 * @param viewGroup
	 * @param enabled
	 */
	public static void enableDisableViewGroup(ViewGroup viewGroup,
			boolean enabled) {
		int childCount = viewGroup.getChildCount();
		for (int i = 0; i < childCount; i++) {
			View view = viewGroup.getChildAt(i);

			if (view.getId() != R.id.bt_left) {
				view.setEnabled(enabled);
				if (view instanceof ViewGroup) {
					enableDisableViewGroup((ViewGroup) view, enabled);
				}
			}
		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {

		case R.id.bt_left:
			menuLeftSlideAnimationToggle();
			if (mPopupWindow != null && mPopupWindow.isShowing()) {
				mPopupWindow.dismiss();
			}
			
			isPopUp=false;
			break;
		//case R.id.bt_right:
		//	menuRightSlideAnimationToggle();
		//	break;
		case R.id.bt_me:
			if(!isPopUp)
			{
				View popupView = getLayoutInflater().inflate(R.layout.popup_window, null);
	
				mPopupWindow = new PopupWindow(popupView,
						(int)(metrics.widthPixels/1.5), LayoutParams.WRAP_CONTENT);
				mPopupWindow.setWidth((int) ((int)metrics.widthPixels*0.9));
				mPopupWindow.setHeight((int) ((int)metrics.heightPixels*0.7));
				
				/**
				 * PopupWindow Show 硫붿꽌�뱶
				 * showAsDropDown(anchor, xoff, yoff)
				 * @View anchor : anchor View瑜� 湲곗��쑝濡� PopupWindow �몴�떆 (�긽,�븯)
				 * PopupWindow媛� 理쒕��븳 �솕硫댁뿉 �몴�떆�릺�룄濡� �떆�뒪�뀥�씠 �꽕�젙�빐 以��떎.
				 * xoff, yoff : anchor View瑜� 湲곗��쑝濡� PopupWindow瑜� �몴�떆�맂寃껋쓣
				 * 湲곗��쑝濡� xoff�뒗 x醫뚰몴, yoff�뒗 y醫뚰몴 留뚰겮 �씠�룞 �븳�떎.
				 * @int xoff : -�닽�옄(�솕硫� �쇊履쎌쑝濡� �씠�룞), +�닽�옄(�솕硫� �삤瑜몄そ�쑝濡� �씠�룞)
				 * @int yoff : -�닽�옄(�솕硫� �쐞履쎌쑝濡� �씠�룞), +�닽�옄(�솕硫� �븘�옒履쎌쑝濡� �씠�룞)
				 * achor View 瑜� �뜮�뒗 寃껊룄 媛��뒫
				 * �솕硫대컮源� 醫뚯슦, �쐞�븘�옒濡� �씠�룞 媛��뒫 (吏ㅻ┛ �긽�깭濡� �몴�떆�맖)
				 */
				mPopupWindow.setAnimationStyle(-1); // �븷�땲硫붿씠�뀡 �꽕�젙(-1:�꽕�젙�븞�븿, 0:�꽕�젙)
				mPopupWindow.showAtLocation(popupView, Gravity.CENTER | Gravity.TOP, 0, 0);
				
				/**
				 * update() 硫붿꽌�뱶瑜� �넻�빐 PopupWindow�쓽 醫뚯슦 �궗�씠利�, x醫뚰몴, y醫뚰몴
				 * anchor View源뚯� �옱�꽕�젙 �빐以꾩닔 �엳�뒿�땲�떎.
				 */
	//			mPopupWindow.update(anchor, xoff, yoff, width, height)(width, height);
				isPopUp=true;
				break;
			}
			else
			{
				if (mPopupWindow != null && mPopupWindow.isShowing()) {
					mPopupWindow.dismiss();
				}
				isPopUp=false;
				break;
			}
		}

	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
